﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using fetco.entities;

namespace fetco.data
{
    public class WebSessionDAO
    {
        private static WebSessionDAO _instance;
        private Connector _conn;

        public WebSessionDAO()
        {
            this._conn = Connector.GetInstance();
        }
        public static WebSessionDAO GetInstance()
        {
            if (_instance == null)
            {
                _instance = new WebSessionDAO();
            }
            return _instance;
        }

        #region Get Data
        public long GetTime(long user_id)
        {
            string output = WebSessionField.Time.ToString();
            string condition = WebSessionField.UserId.ToString() + "=" + user_id;
            string ordering = WebSessionField.Time.ToString();
            string query = @"select " + output + " from " + WebSession.TableName + " where " + condition + " order by " + ordering + " " + OrderByDirection.DESC.ToString();
            SqlDataReader reader = this._conn.ExcuteReader(query);
            if(reader!=null && reader.Read())
            {
                if (!reader.IsDBNull(reader.GetOrdinal(WebSessionField.Time.ToString())))
                    return (long)reader[WebSessionField.Time.ToString()];
            }
            return 0;
        }
        public long GetTime(string login_name)
        {
            string output = WebSessionField.Time.ToString();
            string condition = WebSessionField.LoginName.ToString() + "='" + login_name + "'";
            string ordering = WebSessionField.Time.ToString() + " " + OrderByDirection.DESC.ToString();
            string query = @"select " + output + " from " + WebSession.TableName + " where " + condition + " order by " + ordering;
            SqlDataReader reader = this._conn.ExcuteReader(query);
            if (reader != null && reader.Read())
            {
                if (!reader.IsDBNull(reader.GetOrdinal(WebSessionField.Time.ToString())))
                    return (long)reader[WebSessionField.Time.ToString()];
            }
            return 0;
        }
        #endregion

        #region Set Data
        public void SetSession(WebSession websession)
        {
            string input =  WebSessionField.UserId + ","+
                            WebSessionField.LoginName+","+
                            WebSessionField.Time;
            string value =  websession.UserId + "," +
                        "'" + websession.LoginName + "'," +
                            websession.Time;
            string query = @"insert into " + WebSession.TableName + " (" + input + ") values(" + value + ")";
            if (this._conn.ExcuteNonQuery(query) > 0)
            {
                websession.SessionId =(long) this._conn.GetMaxValue(WebSessionField.SessionId.ToString(),WebSession.TableName);
            }
        }
        #endregion

        #region Delete Data
        public bool DelSession(long time)
        {
            string condition = WebSessionField.Time.ToString() + "<" + time.ToString();
            string query = @"delete from " + WebSession.TableName + " where " + condition;
            if (this._conn.ExcuteNonQuery(query) > 0)
            {
                return true;
            }
            return false;
        }

        public bool DelSessionBy(long user_id)
        {
            string condition = WebSessionField.UserId.ToString() + "=" + user_id;
            string query = @"delete from " + WebSession.TableName + " where " + condition;
            if (this._conn.ExcuteNonQuery(query) > 0)
            {
                return true;
            }
            return false;
        }

        public bool DelSessionBy(string login_name)
        {
            string condition = WebSessionField.LoginName.ToString() + "='" + login_name + "'";
            string query = @"delete from " + WebSession.TableName + " where " + condition;
            if (this._conn.ExcuteNonQuery(query) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region Update Data
        
        public bool UpdateSession(long user_id,long time)
        {
            string value = WebSessionField.Time.ToString() + "=" + time;
            string condition = WebSessionField.UserId.ToString() + "=" + user_id;
            string query = @"update " + WebSession.TableName + " set " + value + " where " + condition;
            if (this._conn.ExcuteNonQuery(query) > 0)
            {
                return true;
            }
            return false;
        }

        public bool UpdateSession(string login_name, long time)
        {
            string value = WebSessionField.Time.ToString() + "=" + time;
            string condition = WebSessionField.LoginName.ToString() + "='" + login_name + "'";
            string query = @"update " + WebSession.TableName + " set " + value + " where " + condition;
            if (this._conn.ExcuteNonQuery(query) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion
    }
}
